<template>
    <div>
        <div class="container">
            <el-main>
                <el-form ref="newsTagFormRef" :rules="newsTagFormRules" :model="newsTagForm" :label-width="formLabelWidth">
                    <el-form-item label="ID" prop="id" v-if="false">
                        <el-input v-model="newsTagForm.id" c/>
                    </el-form-item>
					<!-- 资讯常规信息 -->
                    <el-form-item label="标签名称" prop="tagName">
                        <el-input v-model="newsTagForm.tagName" clearable class="handle-input mr10" />
                    </el-form-item>
                    <el-form-item label="标签别名" prop="tagEname">
                        <el-input v-model="newsTagForm.tagEname" clearable class="handle-input mr10" />
                    </el-form-item>
                    <el-form-item label="ICON" prop="icon">
                        <el-input v-model="newsTagForm.icon" clearable class="handle-input mr10" />
                    </el-form-item>
                </el-form>
            </el-main>
			<el-footer>
                <el-button type="primary" @click="func_onSubmit" plain>保存数据</el-button>
                <el-button type="success" @click="func_onReset" plain>重置表单</el-button>
                <el-button @click="func_closeDialog" plain>取 消</el-button>
			</el-footer>
        </div>
    </div>
</template>

<script>
import { onMounted, ref, reactive, toRefs, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from "element-plus";
import { findNewsTagInfoById, addNewsTag, saveNewsTag } from "../../../../api/hairmanager/newsTag";

import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../../api/CommonErrorMessage.js";

export default {
    name: "newsTagForm",
	props:{
        selectValue: Object,
	},
    emits: ["newsTagData"],
    setup(props, cex){
		onMounted(() => {
		});
        const router = useRouter();
		const route = useRoute();
        const formLabelWidth = ref("170px");
		/**
         * @name 定义业务表单
         */
        const getInitFormData = () => {
            return {
				id: 0,
                tagName: '',
                tagEname: '',
                icon: '',
            }
        };
        /**
         * @name 表单初始化
         */
        const resetnewsTagForm = () => {
            Object.assign(newsTagForm, getInitFormData());
            sortNum.value = 1;
        };
		// 新闻资讯表单数据
        const newsTagForm = reactive(getInitFormData());
        const newsTagFormRef = ref(null);
        const newsTagFormRules = {
            tagName: [
                { required: true, message: "请输入资讯标签名称", trigger: "blur" },
            ],
            tagEname: [
                { required: true, message: "请输入资讯标签英文字", trigger: "blur" },
            ],
        };
        const sortNum = ref(0);
        const loadDataFlg = ref(false);
        // =========================================================================================
        // 提交
        const func_onSubmit = () => {
            // 表单校验
            newsTagFormRef.value.validate((valid) => {
                if (valid) {
                    if(loadDataFlg.value){
                        saveNewsTag(newsTagForm).then((res) => {
                            if(res.code != 200) {
                                if(res.type == 0) {
                                    ElMessage.warning(res.message);
                                } else {
                                    ElMessage.error(res.message);
                                }
                            } else {
                                ElMessage.success(SUCCESS_EDITDATA_DIALOG_MESSAGE);
                                cex.emit('newsTagData', {state: 1, data: []});
                            }
                        });
                    } else {
                        addNewsTag(newsTagForm).then((res) => {
                            if(res.code != 200) {
                                if(res.type == 0) {
                                    ElMessage.warning(res.message);
                                } else {
                                    ElMessage.error(res.message);
                                }
                            } else {
                                ElMessage.success(SUCCESS_ADDDATA_DIALOG_MESSAGE);
                                cex.emit('newsTagData', {state: 1, data: []});
                            }
                        });
                    }
                    resetnewsTagForm();
                } else {
                    return false;
                }
            });
        };
        // 重置
        const func_onReset = () => {
            resetnewsTagForm();
        };
        const func_closeDialog = () => {
            cex.emit('newsTagData', {state: 1, data: []});
        };
        // =========================================================================================
        // 业务数据监听
        watch(() => props.selectValue, (val) => {
			if(val['load']){
                findNewsTagInfoById(props.selectValue['id']).then((res) => {
                    Object.keys(newsTagForm).forEach((item) => {
                        newsTagForm[item] = res.data[item];
                    })
                    sortNum.value = newsTagForm['sort'];
                });
			} else {
                resetnewsTagForm();
            }
            loadDataFlg.value = val['load'];
        },{deep: true,immediate: true});
        return {
			// 系统变量
            formLabelWidth, newsTagForm, newsTagFormRef, newsTagFormRules, loadDataFlg,
			// 主业务调用方法
            getInitFormData, resetnewsTagForm, func_onSubmit, func_onReset, func_closeDialog,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 180px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>